#ifndef _SOCKETENV_HPP
#define _SOCKETENV_HPP

#include <WinSock2.h>
#include <Windows.h>
#include <iostream>

//critical_section wrapper
class CriticalSectionWrapper
{
public:
	CriticalSectionWrapper()
	{
		::InitializeCriticalSectionAndSpinCount(&_cs, 10);
	}
	~CriticalSectionWrapper()
	{
		::DeleteCriticalSection(&_cs);
	}
	void Lock()
	{
		::EnterCriticalSection(&_cs);
	}
	void UnLock()
	{
		::LeaveCriticalSection(&_cs);
	}
private:
	CRITICAL_SECTION _cs;
};

class SocketEnv
{
public:
	static SocketEnv* getinstance();
	~SocketEnv();
private:
	SocketEnv();
	SocketEnv(const SocketEnv&);
	SocketEnv &operator=(const SocketEnv&);

	WSADATA _wsaData;
	static SocketEnv *_instance;
	static CriticalSectionWrapper _csw;
};

inline SocketEnv::SocketEnv()
{
	int res;
	res = WSAStartup(MAKEWORD(2, 2), &_wsaData);
	if (res != 0)
	{
		std::cerr << "WSAStartup() error" << std::endl;
		exit(1);
	}
}

#endif
